import Vue from 'vue'
import Vuex from 'vuex'
import { getItem, setItem } from '@/utils/storage.js'
const TOKEN_KEY = 'TOUTIAO_USER'
const ID_KEY = 'TOUTIAO_ID'
const PTOKEN_KEY = 'TOUTIAO_PUSER'
const PID_KEY = 'TOUTIAO_PID'
Vue.use(Vuex)
export default new Vuex.Store({
  state: {
    // 获取本地数据
    user: getItem(TOKEN_KEY),
    id: getItem(ID_KEY),
    Puser: getItem(PTOKEN_KEY),
    Pid: getItem(PID_KEY)
  },
  mutations: {
    setUser (state, data) {
      state.user = data
      // 保存本地数据
      setItem(TOKEN_KEY, state.user)
    },
    setId (state, userId) {
      state.id = userId
      setItem(ID_KEY, state.id)
    },
    setPuser (state, userId) {
      state.Puser = userId
      setItem(PTOKEN_KEY, state.Puser)
    },
    setPid (state, userId) {
      state.Pid = userId
      setItem(PID_KEY, state.Pid)
    }
  }
})
